local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local base = require "base"
local tabletool = require "tabletool"
local timetool = require "timetool"
require "fishenum"

local filename = "tabletimer.lua"

local TableTimer = {}

function TableTimer.delete_table(timerid, request)
	local server = msghelper:get_server()    
	local table_data = server.table_data
	if table_data.delete_table_timer_id == timerid then
		for _, seat in ipairs(table_data.seats) do
			if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
				server.logic.tablelogic.passive_standuptable(table_data, seat)
			end
		end
		table_data.delete_table_timer_id = -1
        server.delete()
    end
end

function TableTimer.disconnect(timerid, request)
	local server = msghelper:get_server() 
	local table_data = server.table_data
	local seat = table_data.seats[request.seat_index]
	if seat and (seat.disconnect_timer_id == timerid) then
		seat.disconnect_timer_id = -1
		server.logic.tablelogic.passive_standuptable(table_data, seat)
	end
end

function TableTimer.changeshootstat(timerid, request)
	local server = msghelper:get_server() 
	local table_data = server.table_data
	local seat = table_data.seats[request.seat_index]
	if seat and seat.timer_id == timerid then
		seat.timer_id = nil
		if seat.shootstat == 1 then
			seat.shootstat = 0
			msghelper:sendmsg_to_alltableplayer("shootstatN", {
				rid = seat.rid,
				seat_index = seat.index,
				shootstat = seat.shootstat,
			})
		end
	end
end

return TableTimer